package com.yunshipei.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;

import com.yunshipei.enterplorer.R;
import com.yunshipei.model.MainExtraBean;
import com.yunshipei.model.YspEvent;

import butterknife.Bind;
import io.rong.eventbus.EventBus;

public class PadMeFragment extends BaseBodyFragment {

    public static PadMeFragment newInstance(MainExtraBean mainExtraBean) {
        PadMeFragment padMeFragment = new PadMeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARGS_CUSTOMER_MAIN_DATA, mainExtraBean);
        padMeFragment.setArguments(bundle);
        return padMeFragment;
    }

    @Bind(R.id.fl_sub_menu)
    protected FrameLayout subMenu;
    @Bind(R.id.fl_third_part)
    protected FrameLayout thirdMenu;

    private Fragment settingsFragment;
    private Fragment aboutFragment;

    @Override
    public int viewLayout() {
        return R.layout.fragment_pad_me;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        Fragment leftFragment = getChildFragmentManager().findFragmentByTag(MeFragment.class.getName());
        if (leftFragment == null) {
            leftFragment = MeFragment.newInstance(mMainExtraBean);
            getChildFragmentManager().beginTransaction().add(R.id.fl_main_menu, leftFragment, leftFragment.getClass().getName()).commit();
        }
        settingsFragment = getChildFragmentManager().findFragmentByTag(SettingsFragment.class.getName());
        if (settingsFragment == null) {
            settingsFragment = SettingsFragment.newInstance(mMainExtraBean);
            getChildFragmentManager().beginTransaction().add(R.id.fl_sub_menu, settingsFragment, settingsFragment.getClass().getName()).commit();
        }
        aboutFragment = getChildFragmentManager().findFragmentByTag(AboutFragment.class.getName());
        if (aboutFragment == null) {
            aboutFragment = AboutFragment.newInstance(mMainExtraBean);
            getChildFragmentManager().beginTransaction().add(R.id.fl_sub_menu, aboutFragment, aboutFragment.getClass().getName()).commit();
        }
        if (settingsFragment.isHidden()) {
            getChildFragmentManager().beginTransaction().show(settingsFragment).commit();
        }
        if (!aboutFragment.isHidden()) {
            getChildFragmentManager().beginTransaction().hide(aboutFragment).commit();
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    //设置与关于浏览器item切换
    public void onEventMainThread(YspEvent.PadClick padClick) {
        subMenu.setVisibility(View.VISIBLE);
        thirdMenu.setVisibility(View.INVISIBLE);
        String name = padClick.className;
        if (name.equals(AboutFragment.class.getName())) {
            if (aboutFragment.isHidden()) {
                getChildFragmentManager().beginTransaction().show(aboutFragment).commit();
            }
            if (!settingsFragment.isHidden()) {
                getChildFragmentManager().beginTransaction().hide(settingsFragment).commit();
            }
        } else if (name.equals(SettingsFragment.class.getName())) {
            if (!aboutFragment.isHidden()) {
                getChildFragmentManager().beginTransaction().hide(aboutFragment).commit();
            }
            if (settingsFragment.isHidden()) {
                getChildFragmentManager().beginTransaction().show(settingsFragment).commit();
            }
        }
    }

    //进入关于详情页面
    public void onEventMainThread(YspEvent.AboutDetailClick aboutDetailClick) {
        String className = aboutDetailClick.className;
        if (className.equals(AboutDetailFragment.class.getName())) {
            String url = aboutDetailClick.url;
            String name = aboutDetailClick.name;
            Fragment fragment = AboutDetailFragment.newInstance(url, name);
            getChildFragmentManager().beginTransaction().replace(R.id.fl_third_part, fragment, fragment.getClass().getName()).commit();
        }
        showThirdPart();
    }

    //进入字体设置界面
    public void onEventMainThread(YspEvent.FontSizeSetting fontSizeSetting) {
        Fragment fragment = FontSizeSettingFragment.newInstance();
        getChildFragmentManager().beginTransaction().replace(R.id.fl_third_part, fragment, fragment.getClass().getName()).commit();
        showThirdPart();
    }

    //进入swa
    public void onEventMainThread(YspEvent.SWA swa) {
        Fragment fragment = SWAFragment.newInstance(mMainExtraBean);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_third_part, fragment, fragment.getClass().getName()).commit();
        showThirdPart();
    }

    //设置swa用户名密码
    public void onEventMainThread(YspEvent.SWASetting swaSetting) {
        Fragment fragment = SWADetailFragment.newInstance(mMainExtraBean, swaSetting.ssoSettingModel);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_third_part, fragment, fragment.getClass().getName()).commit();
        showThirdPart();
    }

    //修改密码
    public void onEventMainThread(YspEvent.ModifyPwd modifyPwd) {
        Fragment fragment = ModifyPwdFragment.newInstance(mMainExtraBean);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_third_part, fragment, fragment.getClass().getName()).commit();
        showThirdPart();
    }

    private void showThirdPart() {
        subMenu.setVisibility(View.INVISIBLE);
        thirdMenu.setVisibility(View.VISIBLE);
    }
}
